<!DOCTYPE html>
<html>

	<head>
		<meta charset="UTF-8">
		<title></title>
		<script>
			window.onload = function() {
				//获取全选button对象
				var sa = document.getElementById("selectAll");
				console.log(sa);
				//给button按钮添加onclick单击事件
			/*	sa.onclick = function() {
					//alert(123);
					//获取所有的多选框对象，使用name选择器
					var boxs = document.getElementsByName("hobby");
					console.log(boxs);
					for(var i = 0; i < boxs.length; i++) {
						console.log(boxs[i].checked);
						//设置checked属性=true
						boxs[i].checked = true;
					}
				}*/
				sa.onclick = selectAll;
			}

			function selectAll() {
				//获取所有的多选框对象，使用name选择器
				var boxs = document.getElementsByName("hobby");
				console.log(boxs);
				for(var i = 0; i < boxs.length; i++) {
					console.log(boxs[i].checked);
					//设置checked属性=true
					boxs[i].checked = true;
				}
			}
		</script>
	</head>

	<body>
		<button id="selectAll">全选</button>
		<button>全不选</button>
		<button>反选</button>
		<br>
		<input type="checkbox" name="hobby" value="读书" />读书<br />
		<input type="checkbox" name="hobby" value="写字" />写字<br />
		<input type="checkbox" name="hobby" value="睡觉" />睡觉<br />
		<input type="checkbox" name="hobby" value="吃饭" />吃饭<br />
		<input type="checkbox" name="hobby" value="玩游戏" />玩游戏<br />
	</body>

</html>